<template>
	<el-row>
		<el-row>
			<el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">添加</el-button>
		</el-row>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column prop="id" label="#ID" width="180"></el-table-column>
			<el-table-column prop="name" label="姓名" width="180"></el-table-column>
			<el-table-column prop="host" label="主机"></el-table-column>
			<el-table-column prop="port" label="端口"></el-table-column>
			<el-table-column prop="username" label="用户名"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.id)"></el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNo"
			:page-sizes="[50, 100, 150, 200]"
			:page-size="100"
			layout="total, sizes, prev, pager, next, jumper"
			:total="total">
		</el-pagination>

		<el-dialog title="添加" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
			<el-form :model="form">
				<el-form-item>
					<el-input autocomplete="off" v-model="form.name">
						<template slot="prepend">名称</template>
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-input autocomplete="off" v-model="form.host">
						<template slot="prepend">主机</template>
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-input autocomplete="off" v-model="form.port">
						<template slot="prepend">端口</template>
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-input autocomplete="off" v-model="form.username">
						<template slot="prepend">用户名</template>
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-input autocomplete="off" v-model="form.password" type="password">
						<template slot="prepend">密码</template>
					</el-input>
				</el-form-item>
				<el-form-item>
					<el-input autocomplete="off" v-model="form.dbName">
						<template slot="prepend">数据库</template>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submit">确 定</el-button>
			</div>
		</el-dialog>
	</el-row>
</template>

<script>
	export default {
		name: 'Connection',
		data() {
			return {
				tableData: [],
				pageNo: 1,
				total: 0,
				pageSize : 50,
				dialogVisible: false,
				form: {
					name: '',
					host: '',
					port: 3306,
					username: '',
					password:'',
					dbName: ''
				}
			}
		},
		methods: {
			async submit(){
				const {code} = await this.$http.post('/db/connection/save', this.form);
				if(code === 200){
					this.load();
					this.dialogVisible = false;
				}
			},
			del(id){
				this.$http.delete(`/db/connection/${id}`).then(()=>{
					this.load();
				});
			},
			handleSizeChange(v){
				this.pageSize = v;
				this.load();
			},
			handleCurrentChange(v){
				this.pageNo = v;
				this.load();
			},
			handleClose:function(){
				this.dialogVisible = false;
			},
			add(){
				this.dialogVisible = true;
			},
			async load(){
				const params = {
					pageNo: this.pageNo,
					pageSize: this.pageSize
				};
				const {data} = await this.$http.get('/db/connections', {params: params});
				this.tableData = data.data;
				this.total = data.total;
			}
		},
		mounted() {
			this.load();
		}
	}
</script>

<style scoped lang="less">

</style>
